#! /usr/bin/python3
# coding=utf-8
# @Time:2023/10/17 23:31
# @Author: zhangtiejun
# import pymysql
from common.yaml_config import GetConf


class MySqlOperate:
    # 获取连接配置
    def __init__(self):
        mysql_con = GetConf().get_mysql_config()
        self.host = mysql_con["host"]
        self.db = mysql_con["db"]
        self.port = mysql_con["port"]
        self.user = mysql_con["user"]
        self.password = mysql_con["password"]
        self.conn = None
        self.cur = None
        # 私有方法

    # 连接数据库
    def __conn_db(self):
        try:
            # self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, db=self.db,
            #                             port=self.port,
            #                             charset="utf8")
            self.conn=None
        except Exception as e:
            print(e)
            return False
        self.cur = self.conn.cursor()
        return True

    # 关闭数据库
    def __close_conn(self):
        self.cur.close()
        self.conn.close()
        return True

    # 将数据提交
    def __commit(self):
        self.conn.commit()
        return True

    # 查询
    def query(self, sql):
        self.__conn_db()
        self.cur.execute(sql)
        query_data = self.cur.fetchall()
        if query_data == ():
            query_data = None
            print("没有获取到数据，表为空")
        else:
            pass
        self.__close_conn()
        return query_data

    # 曾、删、改
    def insert_update_table(self, sql):
        self.__conn_db()
        self.cur.execute(sql)
        self.__commit()
        self.__close_conn()


if __name__ == '__main__':
    print(MySqlOperate().query("select * from user"))